package com.mtiiot.fstcpserver.netty;

import com.mtiiot.fstcpserver.base.BaseService;
import com.mtiiot.fstcpserver.fastdfs.FastdfsBase;
import com.mtiiot.fstcpserver.fastdfs.core.NameValuePair;
import com.mtiiot.fscore.bean.MetadataInfo;
import com.mtiiot.fscore.bean.elasticsearch.metadata.NettyMetadata;
import com.mtiiot.fscore.inter1.NettyService1;
import com.mtiiot.fscore.utils.RequestType;
import com.mtiiot.fstcpserver.utils.Constants;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xyr on 2016/8/24.
 */
public class NettyServiceImpl1 extends BaseService implements NettyService1 {
    private static final Logger logger = LogManager.getLogger(NettyServiceImpl1.class);
    @Autowired
    private FastdfsBase fastdfsBase;

    @Override
    public String storeFile(MetadataInfo metadataInfo, String deviceId, String seqNo, String code, String ext, byte[] file) {
        String dfsKey="";
        try {
            // 检验元数据
            checkMetadata(metadataInfo);
            NameValuePair[] nvp =new NameValuePair[1];
            nvp[0]=new NameValuePair("ext",ext);
            // 上传文件到分布式文件系统
            try {
                dfsKey = fastdfsBase.uploadFile(file, ext,nvp); // 测试用暂时注释掉
            } catch (Exception e){
                NettyMetadata nettyMetadata = new NettyMetadata();
                putData(metadataInfo,nettyMetadata,deviceId,seqNo,code,ext,file.length/1024,"");
                metadataInQueue(Constants.TOPIC_METADATAINFO, Constants.TYPE_METADATA_FASTDFS, nettyMetadata);
            }
            NettyMetadata nettyMetadata = new NettyMetadata();
            //setMetadataBeanData(metadataInfo,nettyMetadata, RequestType.INSERT.getName());
            double fileSize=file.length/1024;
            if(fileSize<1){
                fileSize=1;
            }

            /*nettyMetadata.setDeviceId(deviceId);
            nettyMetadata.setCode(code);
            nettyMetadata.setSeqNo(seqNo);

            nettyMetadata.setDfsId(dfsKey);
            nettyMetadata.setExt(ext);
            nettyMetadata.setFileType(Constants.FASTDFS_FILE_TYPE_NETTY);
            nettyMetadata.setFileSize(fileSize);
            // 设置重传的次数
            nettyMetadata.setRetransmissionCount(0);
            // 设置重传的时间
            nettyMetadata.setRetransmissionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));*/

            putData(metadataInfo,nettyMetadata,deviceId,seqNo,code,ext,fileSize,dfsKey);
            // 元数据到缓存消息队列
            metadataInQueue(Constants.TOPIC_METADATAINFO, Constants.TYPE_METADATA_FASTDFS, nettyMetadata);
            return dfsKey;
        } catch (Exception ex){
            //异常信息 元数据入reids队列
            handleException(Constants.MODULE_FASTDFS,"NettyServiceImpl1","storeFile",ex,logger);
            return dfsKey;
        }
    }

    // 存入文件到分布式文件系统时候，做元数据的记录
    public void putData(MetadataInfo metadataInfo,NettyMetadata metadata ,String deviceId, String seqNo, String code, String ext,double fileSize,String dfsKey ){
        try {
            setMetadataBeanData(metadataInfo, metadata, RequestType.INSERT.getName());
            metadata.setDeviceId(deviceId);
            metadata.setCode(code);
            metadata.setSeqNo(seqNo);
            metadata.setDfsId(dfsKey);
            metadata.setExt(ext);
            metadata.setFileType(Constants.FASTDFS_FILE_TYPE_NETTY);
            metadata.setFileSize(fileSize);
            // 设置重传的次数
            metadata.setRetransmissionCount(0);
            // 设置重传的时间
            metadata.setRetransmissionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        } catch (Exception e){
            handleException(Constants.MODULE_FASTDFS,"NettyServiceImpl1","storeFile",e,logger);
        }
    }

}
